<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Kotan main controller containing methods to render templates and handle
 * menu rendering.
 * @author bagnz0r
 * @package Kotan
 */

class Controller_Kotan extends Controller_Template {
	
	/**
	 * Default template
	 * @var View
	 */
	public $template = 'templates/default';
	
	/**
	 * Is authentication required for the entire controller?
	 * @var bool
	 */
	public $controller_auth = false;
	
	/**
	 * Is authentication required for a specific action?
	 * @var bool
	 */
	public $action_auth = false;
	
	/**
	 * Executes actions before anything happens
	 * @see system/classes/kohana/controller/Kohana_Controller_Template::before()
	 */
	public function before()
	{
		// Invoke parent
		parent::before();
		
		// If we're rendering template automatically...
		if ($this->auto_render)
		{
			// Set cookie salt
			Cookie::$salt = Kohana::$config->load('kotan.system.cookie_salt');
			
			// Set locale
			$locale = Cookie::get('kotan_lang', 'en-us');
			I18n::lang($locale);	
			
			// Set website title automatically by using blog title and active controller name
			$this->template->title = Kohana::$config->load('kotan.blog.title') . ' :: ' . 
				I18n::get(ucwords(Request::current()->controller()));
				
			// Get empty vars ready for template
			$this->setup_variables();
		}
	}
	
	/**
	 * Executes actions after everything is executed
	 * @see system/classes/kohana/controller/Kohana_Controller_Template::after()
	 */
	public function after()
	{
		// Scripts and styles
		$this->template->scripts = array(
			'https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js',
		);
		$this->template->styles = array(
			'static/styles/default.css',
		);
		
		// Setup the menu
		$this->setup_menu();
		
		// Get menu items from database, if any
		foreach ($this->get_menu_from_database() as $item)
		{
			$this->add_to_menu($item);
		}
		
		// Invoke parent
		parent::after();
	}
	
	/**
	 * Add menu item
	 * @param $item
	 * 
	 * Example:
	 * $item = array(
	 * 	'title' => 'My link',
	 * 	'uri' => 'controller/action/value',
	 * 	'weight' => 8,
	 * );
	 * 
	 * weight array param is the sorting order of an item.
	 * Top element is ascending from 0.
	 */
	public function add_to_menu($item)
	{
		array_push($this->template->menu, $item);
	}
	
	public function get_param($param, $digit = false)
	{
		// Get the param
		$param = Security::xss_clean($this->request->param($param));
			
		// Validate
		if ($digit && !Valid::digit($param))
			return false;
			
		return $param;
	}
	
	/**
	 * Action used for setting locales.
	 */
	public function action_set_lang()
	{
		// Get the locale
		$locale = Security::xss_clean($this->request->param('locale'));

		// Set the locale
		Cookie::set('kotan_lang', $locale);
		
		// Redirect
		Request::current()->redirect('');
	}
	
	/**
	 * Get menu items from database.
	 */
	protected function get_menu_from_database()
	{
		// Instantiate kotan model
		$kotan = new Model_Kotan();
		
		// Invoke get_menu() method
		$menu = $kotan->get_menu();
		
		// Kotan model is no longer needed
		unset($kotan);
		
		// Check if null
		if (is_null($menu))
			return array();
		
		return $menu;
	}
	
	/**
	 * Setup variables.
	 */
	protected function setup_variables()
	{
		$this->template->content = '';
		$this->template->menu = array();
		$this->template->scripts = array();
		$this->template->styles = array();
	}
	
	/**
	 * Setup default menu.
	 * Basically, this method adds menu entries leading to default controllers.
	 */
	protected function setup_menu()
	{
		// Blog
		$this->add_to_menu(array(
			'title' => I18n::get('Blog'),
			'uri' => 'blog',
			'weight' => 0,
		));
	}
}